package com.yjx.managedemo.controller;

import com.yjx.managedemo.entity.Admins;
import com.yjx.managedemo.entity.Employees;
import com.yjx.managedemo.po.AdminQuery;
import com.yjx.managedemo.re.PageResult;
import com.yjx.managedemo.re.Result;
import com.yjx.managedemo.service.AdminsService;
import com.yjx.managedemo.service.EmployeesService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("admin")
public class AdminController {

    @Resource
    private AdminsService adminsService;
    @Resource
    private EmployeesService employeesService;

    /**
     * 前往登陆页面
     * @return
     */
    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }

    /**
     * 登陆验证
     * @param name
     * @param password
     * @param type
     * @return
     */
    @RequestMapping("login")
    @ResponseBody
    public Result login(String name, String password, String type,HttpServletRequest request){
        //用户名，密码，账号类型的非空判断
        if (name==null||name.equals("")){
            return Result.fail();
        }
        if (password==null||password.equals("")){
            return Result.fail();
        }
        if (type==null||type.equals("")){
            return Result.fail();
        }
        if (type.equals("admin")){
            Admins admins = adminsService.login(name,password);
            if (admins==null){
                return Result.fail();
            }
            request.getSession().setAttribute("token",admins);
            return Result.success(admins);
        }else if (type.equals("employee")){
            Employees employees = employeesService.login(name,password);
            if (employees==null){
                return Result.fail();
            }
            request.getSession().setAttribute("token",employees);
            return Result.success(employees);
        }
        return Result.fail();
    }

    /**
     * 前往首页
     * @return
     */
    @RequestMapping("toIndexAdminSuper")
    public String toIndexAdminSuper(){
        return "index_admin_super";
    }

    /**
     * 前往首页
     * @return
     */
    @RequestMapping("toIndexAdminNormal")
    public String toIndexAdminNormal(){
        return "index_admin_normal";
    }

    /**
     * 注销，退出登陆
     * @param request
     * @return
     */
    @RequestMapping("logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("token");
        return  "login";
    }

    /**
     * 响应管理员管理页面
     * @return
     */
    @RequestMapping("toAdminList")
    public String toAdminList(){
        return "admin_list";
    }

    /**
     * 表格数据以及条件查找
     * @param adminQuery
     * @return
     */
    @RequestMapping("queryByAllLimit")
    @ResponseBody
    public PageResult queryByAllLimit(AdminQuery adminQuery){
        return adminsService.queryAllByLimit(adminQuery);
    }

    /**
     * 响应添加页面
     * @return
     */
    @RequestMapping("toAdminAdd")
    public String toAdminAdd(){
        return "admin_add";
    }

    /**
     * 添加管理员
     * @param admins
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public Result add(Admins admins){
        if (admins.getName()==null||admins.getName().equals("")){
            return Result.fail();
        }
        if (admins.getPassword()==null||admins.getPassword().equals("")){
            return Result.fail();
        }
        if (admins.getEmail()==null||admins.getEmail().equals("")){
            return Result.fail();
        }
        if (admins.getPhone()==null||admins.getPhone().equals("")){
            return Result.fail();
        }
        admins = adminsService.insert(admins);
        if (admins==null){
            return Result.fail();
        }
        return Result.success(admins);
    }

    /**
     * 多条删除
     * @return
     */
    @RequestMapping("deleteAll")
    @ResponseBody
    public Result deleteAll(Integer[] ids){
        int index = 0;
        if (ids==null||ids.length==0){
            return Result.fail();
        }
        for (Integer id:ids) {
            index = adminsService.deleteById(id) + index;
        }
        return Result.success();
    }

    /**
     * 删除单行
     * @param adminId
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public Result delete(Integer adminId){
        if (adminId==null){
            return Result.fail();
        }
        Integer index = adminsService.deleteById(adminId);
        if (index==0){
            return Result.fail();
        }
        return Result.success();
    }

    /**
     * 响应更新页面
     * @param adminId
     * @return
     */
    @RequestMapping("toAdminUpdate")
    public String toAdminUpdate(Integer adminId){
        return "admin_update";
    }

    @RequestMapping("queryById")
    @ResponseBody
    public  Result queryById(Integer adminId){
        if (adminId!=null){
            Admins admins = adminsService.queryById(adminId);
            if (admins==null){
                return Result.fail();
            }
            return Result.success(admins);
        }
        return Result.fail();
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(Admins admins){
        if (admins!=null){
            admins = adminsService.update(admins);
            if (admins==null){
                return Result.fail();
            }
            return Result.success(admins);
        }
        return Result.fail();
    }
}
